package aex.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import aex.domain.ArticleCategory;
import aex.domain.Puser;
import aex.domain.Score;
import aex.domain.ShowBox;
import aex.domain.ShowPoint;
import aex.domain.SystemAdmin;
import aex.service.PuserService;
import aex.service.ScoreService;
import aex.service.ShowBoxService;
import aex.service.SystemAdminService;
import aex.service.ArticleCategoryService;
import aex.util.CU;
import aex.util.PageBean;

import com.opensymphony.xwork2.ModelDriven;

@Controller("SystemAdminAction")
@Scope("prototype")
public class SystemAdminAction extends BaseActon implements ModelDriven<SystemAdmin> {

	private static final long serialVersionUID = 1L;

	@Resource(name = SystemAdminService.SERVICE_NAME)
	SystemAdminService adminService;

	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	ArticleCategoryService acService;

	@Resource(name = PuserService.SERVICE_NAME)
	PuserService puserService;
	
	
	@Resource(name = ShowBoxService.SERVICE_NAME)
	ShowBoxService sbService;
	
	@Resource(name = ScoreService.SERVICE_NAME)
	ScoreService scoreService;
	

	SystemAdmin admin = new SystemAdmin();
	public SystemAdminAction() {
		CU.deBugPrintln("AdminAction 被创建");
	}

	@Override
	public SystemAdmin getModel() {
		return admin;
	}


	/**
	 * 处理用户登录请求
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String login() {
		String yzm = request.getParameter("yzm").trim().toLowerCase();
		if (yzm.equals(request.getSession().getAttribute("yzm").toString()
				.toLowerCase())) {
			SystemAdmin adminLogin = adminService.check(admin);
			if (adminLogin != null) {
				request.getSession().setAttribute("admin", adminLogin);
				request.getSession().setAttribute("loginTime",
						adminLogin.getSaPrevLoginTime().toLocaleString());
				request.getSession().setAttribute("loginIp",
						adminLogin.getSaPrevLoginIp());
				adminLogin.setSaPrevLoginIp(request.getRemoteAddr());
				adminLogin.setSaPrevLoginTime(CU.getSqlTime());
				adminService.update(adminLogin);
				return "index";
			} else {
				request.setAttribute("err", "您输入的用户名或密码不正确！");
			}
		} else {
			request.setAttribute("err", "验证码错误！");
		}
		return "login";
	}

	
	/**
	 * 进入管理员列表
	 * 
	 * @return
	 */
	public String adminList() {

		admin = (SystemAdmin) request.getSession().getAttribute("admin");
		if (admin.getSaIsRoot() == 2) {// 普通管理员无权进入管理中心
			PageBean<SystemAdmin> pageBean = new PageBean<SystemAdmin>();
			PageBean.initPageBean(pageBean, request, "");
			List<SystemAdmin> list=new ArrayList<SystemAdmin>();
			list.add(admin);
			pageBean.setObjList(list);
			pageBean.setItemCount(1);
			pageBean.setPageCount(1);
			request.setAttribute("pageBean", pageBean);
			return "adminList";
		} else {
			PageBean<SystemAdmin> pageBean = new PageBean<SystemAdmin>();
			PageBean.initPageBean(pageBean, request, "");
			pageBean = adminService.findAllManage(pageBean);
			request.setAttribute("pageBean", pageBean);
			return "adminList";
		}
	}

	/**
	 * 添加一个新的管理员
	 * @return
	 */
	public String addNewAdmin() {

		List<SystemAdmin> adminList = adminService.findAllManageNoPage();
		for (SystemAdmin systemAdmin : adminList) {
			if (systemAdmin.getSaAccount().equals(admin.getSaAccount())) {
				return CU.goMsgPage(request, "账号【" + admin.getSaAccount()
						+ "】已经存在！添加失败！", "err",
						"manage/admin_adminList.do");
			}
		}
		admin.setSaRegisterTime(CU.getSqlTime());
		admin.setSaPrevLoginIp("您是第一次登陆！");
		admin.setSaPrevLoginTime(CU.getSqlTime());
		adminService.save(admin);
		return CU.goMsgPage(request, "成功添加管理员!【" + admin.getSaAccount() + "】",
				"ok", "manage/admin_adminList.do");
	}

	/**
	 * 删除一个用户
	 * 
	 * @return
	 */
	public String delAdmin() {
		SystemAdmin delAdmin = adminService.findById(Integer.parseInt(request.getParameter("id")));
		if (delAdmin.getSaIsRoot() == 0) {
			ajaxPrint("超级管理员无法删除！");
			return null;
		} else {
			adminService.deleteById(delAdmin.getSaId());
			ajaxPrint("删除成功！");
			return null;
		}
	}

	/**
	 * 批量删除管理员
	 * 
	 * @return
	 */
	public String delAdmins() {
		String[] ids = request.getParameter("ids").split(",");
		for (int i = 0; i < ids.length; i++) {
			SystemAdmin delAdmin = adminService.findById(ids[i]);
			if (delAdmin.getSaIsRoot() == 0) {
				ajaxPrint("超级管理员无法删除！");
				return null;
			} else {
				adminService.deleteById(delAdmin.getSaId());
			}
		}
		ajaxPrint("成功删除"+ids.length+ "个管理员");
		return null;
	}

	/**
	 * 跳转到更新用户的密码
	 * 
	 * @return
	 */
	public String goUpdataPassword() {
		 int id=Integer.parseInt(request.getParameter("id"));
		 SystemAdmin currentAdmin=(SystemAdmin) request.getSession().getAttribute("admin");
		 if(currentAdmin.getSaId()==id){
			 request.setAttribute("id",id);
			 return "user-password-edit";
		 }else{
			 return CU.goMsgPage(request, "不能修改别人的密码！", "err", "javascript:;;");
		 }
	}

	/**
	 * 更新用户的密码
	 * 
	 * @return
	 */
	public String updatePassword() {
		try {
			SystemAdmin a = adminService.findById(Integer.parseInt(request.getParameter("id")));
			
			a.setSaPassword(request.getParameter("password"));
			adminService.update(a);
			response.getWriter().write("{\"info\":\"修改成功\",\"status\":\"y\"}");

		} catch (IOException e) {
			try {
				response.getWriter().write(
						"{\"info\":\"修改失败\",\"status\":\"y\"}");
			} catch (IOException e1) {
			}
		}
		return null;
	}

	/**
	 * 退出系统
	 * 
	 * @return
	 */
	public String exit() {
		request.getSession().invalidate();
		return "exit";
	}

	/**
	 * 处理用户登录请求
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String adminUserLogin() {
		
		Puser puser=new Puser();
		puser.setPuAccount(request.getParameter("puAccount"));
		puser.setPuPassword(request.getParameter("puPassword"));
		String yzm = request.getParameter("yzm").trim().toLowerCase();
		if (yzm.equals(request.getSession().getAttribute("yzm").toString()
				.toLowerCase())) {
			Puser userLogin = puserService.check(puser);
			if (userLogin != null) {
					request.getSession().setAttribute("user", userLogin);
					return "welcomeAdminUser";
			} else {
				request.setAttribute("err", "您输入的用户名或密码不正确！");
			}
		} else {
			request.setAttribute("err", "验证码错误！");
		}
		return "adminUserLogin";
	}
	
	public String welcomeAdminUser(){
		if(request.getSession().getAttribute("user")==null){
			return "adminUserLogin";
		}else{
			return "welcomeAdminUser";
		}
	}
	
	public String scoreList(){
		PageBean<Score> pageBean = new PageBean<Score>();
		PageBean.initPageBean(pageBean, request, " ");
		pageBean=scoreService.findAll(pageBean);
		request.setAttribute("pageBean", pageBean);
		return "scoreList";
	}
	

	/**
	 * 更新操作分值
	 */
	public String updateScore(){
		Score score=scoreService.findByID(request.getParameter("id"));
		score.setScValue(request.getParameter("score").trim());
		scoreService.update(score);
		ajaxPrint("操作成功！");
		return null;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
